﻿namespace CodingExercises._11_20;

public class TryReadFile
{
    // 安全读取文件内容
    private readonly IFileSystem _fileSystem;
    private readonly ILogger _logger;

    public TryReadFile(IFileSystem fileSystem, ILogger logger)
    {
        _fileSystem = fileSystem;
        _logger = logger;
    }

    public string Method(string fileName)
    {
        try
        {
            return _fileSystem.ReadFile(fileName);
        }
        catch (Exception e)
        {
            _logger.Log($"Exception thrown:{e.Message}");
            throw;
        }
        finally
        {
            _logger.Log($"Attempted to read file:{fileName}");
        }
    }
}

public interface IFileSystem
{
    string ReadFile(string fileName);
}

public interface ILogger
{
    void Log(string message);
}